
function frac =dataAnalysisPostQuest (subject, maleStart, femaleStart)


%subject='laura_test_Adaptive_50ms_300trials'; %laura_tpp25ms_22_29_300';

qtyTrials=350;%20;
sequenceSize=20;
%maleStart=22;%21;
%femaleStart=29;%30; 

filename=strcat('RAW_',subject);

file=fopen(filename); 
data=fread(file, [4+sequenceSize qtyTrials]);
fclose(file);


%total right
win=0;

%noise=zeros(qtyTrials,sequenceSize);
subsampleMaleStim=[];
subsampleMaleAns=[];
subsampleMaleRight=[];
subsampleMaleWrong=[];
subsampleFemaleStim=[];
subsampleFemaleAns=[];
subsampleFemaleRight=[];
subsampleFemaleWrong=[];
subsampleConfident=[];
subsampleNonconfident=[];


for i=51:qtyTrials
    
    newIndex=i-50;
   
    noise(newIndex,:)=(data(3:2+sequenceSize,i)-data(2,i))';
    
    
        if(data(2,i)==maleStart)
            subsampleMaleStim=[subsampleMaleStim; noise(newIndex,:)]; 
            if(data(sequenceSize+3,i)==1)
                subsampleMaleRight=[subsampleMaleRight; noise(newIndex,:)];
                win=win+1;
            else if(data(sequenceSize+3,i)==0)
                    subsampleMaleWrong=[subsampleMaleWrong; noise(newIndex,:)];
                end
            end 
        else if(data(2,i)==femaleStart)
                subsampleFemaleStim=[subsampleFemaleStim; noise(newIndex,:)];
                if(data(sequenceSize+3,i)==0)
                    subsampleFemaleRight=[subsampleFemaleRight; noise(newIndex,:)];
                    win=win+1;
                else if(data(sequenceSize+3,i)==1)
                    subsampleFemaleWrong=[subsampleFemaleWrong; noise(newIndex,:)];
                   end
                end
            end
        end
    
    if(data(sequenceSize+3,i)==1)
        subsampleMaleAns=[subsampleMaleAns; noise(newIndex,:)]; 
    else if(data(sequenceSize+3,i)==0)
        subsampleFemaleAns=[subsampleFemaleAns; noise(newIndex,:)];
        end
    end
        
    
    if(data(sequenceSize+4,i)==1)
        subsampleConfident=[subsampleConfident; noise(newIndex,:)];
    else subsampleNonconfident=[subsampleNonconfident; noise(newIndex,:)];
    end
    
    %if ((data(2,i)==maleStart && data(sequenceSize+3,i)==1) || (data(2,i)==femaleStart && data(sequenceSize+3,i)==0))
    %    win=win+1;
    %end
    success(newIndex)=win/newIndex;
    
    
   
end


frac=win/qtyTrials

xAxis=1:sequenceSize;

kernel=mean(subsampleMaleRight)+mean(subsampleMaleWrong)-mean(subsampleFemaleRight)-mean(subsampleFemaleWrong);

%size(subsampleMaleRight)
%size(subsampleMaleWrong)
%size(subsampleFemaleRight)
%size(subsampleFemaleWrong)

plot(xAxis,mean(noise),xAxis,kernel);

plot(xAxis,mean(subsampleMaleStim),xAxis,mean(subsampleMaleRight)-mean(subsampleFemaleWrong));
%plot(xAxis,mean(subsampleMaleStim),xAxis,mean(subsampleMaleRight)-mean(subsampleFemaleWrong),xAxis,mean(subsampleMaleRight)-mean(subsampleFemaleWrong)-mean(subsampleMaleStim));


%confidence
%
%plot(xAxis,mean(noise),xAxis,mean(subsampleConfident)-mean(subsampleNonconfident));


%other tests
%
%size(subsampleConfident)
%mean(subsampleConfident(:))
%size(subsampleNonconfident)
%mean(subsampleNonconfident(:))

%mean(subsampleMaleRight(:))
%mean(subsampleMaleWrong(:))
%mean(subsampleFemaleWrong(:)) 
%mean(subsampleFemaleRight(:))


%success evolution
%
%trials=1:qtyTrials;
%plot(trials,success); %evolution of success
 


%this is to look at mu0 AFTER the data was taken (for subjects with no calibration)
%
%
%calibIndices=mean(data(3:2+sequenceSize,:));
%calibAnswers=data(sequenceSize+3,:);
%
%hist(mean(noise').*data(sequenceSize+3,:)); % what is this?!
%plot(calibIndices,calibAnswers,'x'); %male or female answer as a function of stimulus
%
%
%
%qtyWindows=20;
% step=1;
% for winIndex=1:qtyWindows
%     
%     trialsInWin(winIndex)=0;
%     malesInWin(winIndex)=0;
%     window=15+(winIndex)*step;
%     windows(winIndex)=window;
%     
%     
%     for trial=1:qtyTrials
%         if(calibIndices(trial)>= window && calibIndices(trial) < window+step)
%             trialsInWin(winIndex)=trialsInWin(winIndex)+1;
%                 if(calibAnswers(trial)==1)
%                     malesInWin(winIndex)=malesInWin(winIndex)+1;
%                 end
%         end
%     end
%     
%     fracInWin(winIndex)=malesInWin(winIndex)/trialsInWin(winIndex);
%     
% end
%
%
%trialsInWin
%malesInWin
%plot(windows,fracInWin,'x');




end

